/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/


namespace Oceanside
{
    public partial class HowManyBox : ModalWindow
    {
        private static int _lastNumberSelection = 0;
        public static int LastNumberSelection
        {
            get { return HowManyBox._lastNumberSelection; }
            set { HowManyBox._lastNumberSelection = value; }
        }

        public delegate void OkClickedDelegateType(int number);
        public OkClickedDelegateType OkDelegateClicked;

        /// <summary>
        /// 
        /// </summary>
        public HowManyBox(bool isPartyCount = false)
            : base()
        {
            PosWindow.MainPosWindow.Topmost = false;
            InitializeComponent();
            LastNumberSelection = 0;
            if (isPartyCount)
            {
                labelQuestion.Content = "Party Count?";
            }
            else
            {
                labelQuestion.Content = "How Many?";
            }
            keypad.SetNumericKeyCallbackDel(new KeyPad.NumericKeyCallbackDelType(NumericKeyPressed_NOTHROW));
            keypad.SetClearKeyCallbackDel( new KeyPad.ClearCallbackDelType(ClearKeyPressed_NOTHROW));
            keypad.SetOkKeyCallbackDel(new KeyPad.OkCallbackDelType(OkKeyPressed_NOTHROW));
            this.Closing += new System.ComponentModel.CancelEventHandler(MessageBox_Closing_NOTHROW);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void MessageBox_Closing_NOTHROW(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                ModalDialogIsOpen = false;
                CurrentlyOpenInstance = null;
#if !_DEBUG
                PosWindow.MainPosWindow.Topmost = true;

#endif
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_HOWMANY_CLOSING, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param Name="val"></param>
        private void NumericKeyPressed_NOTHROW(int val)
        {
            try
            {
                ///See if the table Number has exceeded 3 digits already as 4 is the max
                if (_lastNumberSelection < 1000)
                {
                    if (!(_lastNumberSelection == 0 && val == 0))
                    {
                        ///Shift the Number to the left and add the new value in.
                        _lastNumberSelection = _lastNumberSelection * 10 + val;
                        textBoxHowMany.Text = _lastNumberSelection.ToString();
                    }
                }
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_HOWMANY_NUMERIC_PRESSED, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void ClearKeyPressed_NOTHROW()
        {
            try
            {
                textBoxHowMany.Text = "";
                _lastNumberSelection = 0;
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_HOWMANY_CLEAR_PRESSED, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void OkKeyPressed_NOTHROW()
        {
            try
            {
                if (OkDelegateClicked != null)
                {
                    OkDelegateClicked(_lastNumberSelection);
                }
                this.Close();
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_HOWMANY_OK_PRESSED, exc);
            }
        }
    }
}
